<template>
  <div class="content">
    <div class="login">
      <p style="font-weight:bold" v-if="isEdit">请输入新密码</p>
      <el-form
        ref="ruleFormRef"
        style="max-width: 600px"
        :model="ruleForm"
        :rules="rules"
        label-width="auto"
        class="demo-ruleForm"
        :size="formSize"
        status-icon
      >
        <el-form-item label prop="account" v-if="!isEdit">
          <el-input
            v-model="ruleForm.account"
            placeholder="账户"
            style="height:35px"
            :prefix-icon="User"
          ></el-input>
        </el-form-item>
        <el-form-item label prop="password" v-if="!isEdit">
          <el-input
            v-model="ruleForm.password"
            placeholder="密码"
            style="height:35px"
            :prefix-icon="UserFilled"
            type="password"
          />
        </el-form-item>
        <el-form-item label prop="newPassword1" v-if="isEdit">
          <el-input
            v-model="ruleForm.newPassword1"
            placeholder="请输入6-16位密码, 区分大小写"
            style="height:35px"
            type="password"
          />
        </el-form-item>
        <el-form-item label prop="newPassword2" v-if="isEdit">
          <el-input
            v-model="ruleForm.newPassword2"
            placeholder="请确认密码"
            style="height:35px"
            type="password"
          />
        </el-form-item>
      </el-form>
      <el-text type="danger" v-if="false">账号或密码错误，请重新输入！</el-text>
      <el-button
        type="primary"
        style="width:100%"
        @click="submitForm(ruleFormRef)"
        v-if="!isEdit"
      >登&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;录</el-button>
      <el-button
        type="primary"
        style="width:100%"
        @click="editPassword(ruleFormRef)"
        v-if="isEdit"
      >修改密码</el-button>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { reactive, ref } from 'vue'
import type { ComponentSize, FormInstance, FormRules } from 'element-plus'
import { UserFilled, User } from '@element-plus/icons-vue'
import router from '../router'
import { ElMessage } from 'element-plus';
import {userApi} from "@/api/user";

interface RuleForm {
    account:string,
    password:string,
    newPassword1:string,
    newPassword2:string,
}
let isEdit = ref(false)
const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<RuleForm>({
    account:'',
    password:'',
    newPassword1:'',
    newPassword2:'',
})

const rules = reactive<FormRules<RuleForm>>({
    account: [
    { required: true, message: '请输入账户', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
  ],

})


const submitForm = async()=>{
  console.log({
    account:ruleForm.account,
    password:ruleForm.password
  });
  
  let res = await userApi.login({
    account:ruleForm.account,
    password:ruleForm.password
  })

  if(res.meta.code=='2000'){
    sessionStorage.setItem('userInfo',JSON.stringify(res.data))
    router.push({path:'/'})
  }
  console.log(res);
}
</script>
<style lang="less" scoped>
.content {
  width: 100%;
  height: 100%;
  background-image: url('../assets/u2.jpg');
  background-position: center center;
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-size: cover;
  .login {
    padding: 20px;
    width: 400px;
    height: 200px;
    position: fixed;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    margin: auto;
    .edit {
      color: #1890ff;
      text-align: right;
      margin-top: 20px;
      margin-bottom: 20px;
      cursor: pointer;
    }
  }
}
</style>